#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDebug>
#include <QDataStream>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    int n = 888;
    QString str("hello");
    QColor color;
    // 创建文件，将三个变量写入文件
    QFile file("./test");
    file.open(QFile::ReadWrite | QFile::Truncate);
    QDataStream out(&file);
    out << n << str << color;
    /*
    file.write((const char*)&n, sizeof(n));
    file.write((const char*)&str, sizeof(str));
    file.write((const char*)&color, sizeof(color));
    */
    file.close();


    // 从文件中读取三个变量，存到
    QFile file1("./test");
    file1.open(QFile::ReadOnly);
    int n1;
    QString str1;
    QColor color1;
    QDataStream in(&file1);
    in >> n1 >> str1 >> color1;
    /*
    file1.read((char*)&n1, sizeof(n1));
    file1.read((char*)&str1, sizeof(str1));
    file1.read((char*)&color1, sizeof(color1));
    */
    file1.close();
    qDebug() << n1 << str1 << color1;
}

MainWindow::~MainWindow()
{
    delete ui;
}
